using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Newtonsoft.Json;
using SimpleJSON;
using Unity.Services.Leaderboards;
using UnityEngine;

namespace Gj.UnityCloud
{
    public class Leaderboard
    {

        public static Dictionary<string, string> Meta = new Dictionary<string, string>() {};

        public static void Update(string identity, int value, Action<bool> callback)
        {
            Debug.Log($"update leaderboard: {identity} {value}");
            if (!Auth.IsLogin()) return;
            Project.ProjectUpdate.Queue.Enqueue(async () =>
            {
                try
                {
                    var playerEntry = await LeaderboardsService.Instance
                        .AddPlayerScoreAsync(identity, value);
                    // .AddPlayerScoreAsync(identity, value, new AddPlayerScoreOptions { Metadata = new Dictionary<string, string>(){{ "team", "red" }}});
                    Debug.Log(JsonConvert.SerializeObject(playerEntry));
                    callback(true);
                    await Task.Delay(5000);
                }
                catch (Exception e)
                {
                    callback(false);
                }
            });
        }

        public static void Number(string identity, Action<JSONObject> callback)
        {
            if (!Auth.IsLogin()) return;
            Project.ProjectUpdate.Queue.Enqueue(async () =>
            {
                Debug.Log($"self leaderboard: {identity}");
                var scoreResponse = await LeaderboardsService.Instance
                    .GetPlayerScoreAsync(identity);
                Debug.Log($"self leaderboard: {JsonConvert.SerializeObject(scoreResponse)}");
                
                var info = new JSONObject();
                info["score"] = scoreResponse.Score;
                info["rank"] = scoreResponse.Rank + 1;
                callback(info);
                await Task.Delay(5000);
            });
        }

        public static void List(string identity, int limit, Action<JSONArray> callback)
        {
            if (!Auth.IsLogin()) return;
            Project.ProjectUpdate.Queue.Enqueue(async () =>
            {
                Debug.Log($"list leaderboard: {identity} {limit}");
                var scorePageResponse = await LeaderboardsService.Instance
                    .GetScoresAsync(identity, new GetScoresOptions { Offset = 0, Limit = limit });
                Debug.Log($"list leaderboard: {JsonConvert.SerializeObject(scorePageResponse)}");
                
                var info = new JSONArray();
                foreach (var one in scorePageResponse.Results)
                {
                    var item = new JSONObject();
                    item["score"] = one.Score;
                    item["rank"] = one.Rank + 1;
                    item["nickName"] = Auth.FormatName(one.PlayerName);
                    info.Add(item);
                }
                callback(info);
                await Task.Delay(5000);
            });

        }

        public static void Info(string identity, int limit, Action<JSONArray> callbackList, Action<JSONObject> callbackSelf)
        {
            if (!Auth.IsLogin()) return;
            Project.ProjectUpdate.Queue.Enqueue(async () =>
            {
                Debug.Log($"list leaderboard: {identity} {limit}");
                var scorePageResponse = await LeaderboardsService.Instance
                    .GetScoresAsync(identity, new GetScoresOptions { Offset = 0, Limit = limit });
                Debug.Log($"list leaderboard: {JsonConvert.SerializeObject(scorePageResponse)}");

                var info = new JSONArray();
                foreach (var one in scorePageResponse.Results)
                {
                    var item = new JSONObject();
                    item["score"] = one.Score;
                    item["rank"] = one.Rank + 1;
                    item["nickName"] = Auth.FormatName(one.PlayerName);
                    info.Add(item);
                }
                callbackList(info);
                await Task.Delay(5000);
                Debug.Log($"self leaderboard: {identity}");
                try
                {
                    var scoreResponse = await LeaderboardsService.Instance
                        .GetPlayerScoreAsync(identity);
                    Debug.Log($"self leaderboard: {JsonConvert.SerializeObject(scoreResponse)}");

                    var self = new JSONObject();
                    self["score"] = scoreResponse.Score;
                    self["rank"] = scoreResponse.Rank + 1;
                    callbackSelf(self);
                    await Task.Delay(5000);
                }
                catch (Exception e)
                {
                    var self = new JSONObject();
                    self["score"] = 0;
                    self["rank"] = 0;
                    callbackSelf(self);
                }
            });

        }
    }
}